/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_seek_proxy.h"
#include "sle_hilog.h"

namespace OHOS {
namespace NearLink {
namespace SleStandard {
int32_t SleSeekProxy::InnerTransact(
    uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("InnerTransact In");
    auto remote = Remote();
    if (remote == nullptr) {
        SLE_LOGE("get remote failed, error code %{public}d", code);
        return OBJECT_NULL;
    }
    int32_t err = remote->SendRequest(code, data, reply, flags);
    if (err != NO_ERROR) {
        SLE_LOGE("send request failed, ipcErr=%{public}d code %{public}d", err, code);
    }
    return err;
}

SleErrorCode SleSeekProxy::RegisterSeekCallback(const sptr<ISleSeekCallback> &callback)
{
    SLE_LOGD("RegisterConnectCallback In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleSeekProxy::GetDescriptor()), SLE_ERR_IPC_TRANS_FAILED, "write interface token failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteRemoteObject(callback->AsObject()), SLE_ERR_IPC_TRANS_FAILED, "write seek callback failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleSeekInterfaceCode::SLE_REGISTER_SEEK_CALLBACK, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    return (SleErrorCode)reply.ReadInt32();
}

SleErrorCode SleSeekProxy::UnregisterSeekCallback(const sptr<ISleSeekCallback> &callback)
{
    SLE_LOGD("UnregisterSeekCallback In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleSeekProxy::GetDescriptor()), SLE_ERR_IPC_TRANS_FAILED, "write interface token failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteRemoteObject(callback->AsObject()), SLE_ERR_IPC_TRANS_FAILED, "write seek callback failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleSeekInterfaceCode::SLE_UNREGISTER_SEEK_CALLBACK, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    return (SleErrorCode)reply.ReadInt32();
}

SleErrorCode SleSeekProxy::StartSeek(SeekParam &param)
{
    SLE_LOGD("StartSeek In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleSeekProxy::GetDescriptor()), SLE_ERR_IPC_TRANS_FAILED, "SleSeekProxy::StartSeek WriteInterfaceToken error");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteParcelable(&param), SLE_ERR_IPC_TRANS_FAILED, "write seek param failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleSeekInterfaceCode::SLE_SEEK_ENABLE, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    return (SleErrorCode)reply.ReadInt32();
}

SleErrorCode SleSeekProxy::StopSeek()
{
    SLE_LOGD("StopSeek In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleSeekProxy::GetDescriptor()), SLE_ERR_IPC_TRANS_FAILED, "SleSeekProxy::StartSeek WriteInterfaceToken error");
    
    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleSeekInterfaceCode::SLE_SEEK_DISABLE, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    return (SleErrorCode)reply.ReadInt32();
}

SleErrorCode SleSeekProxy::GetDefaultSeekParamConfig(SleSeekParam &seekConfig)
{
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleSeekProxy::GetDescriptor()), SLE_ERR_IPC_TRANS_FAILED, "SleSeekProxy::StartSeek WriteInterfaceToken error");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleSeekInterfaceCode::SLE_GET_DEFAULT_SEEK_CONFIG, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    int32_t exception = reply.ReadInt32();
    SLE_ASSERT_AND_RETURN_RET_LOG(exception == NO_ERROR, exception, "get default seek param failed!(%d)", exception);
    seekConfig.addrType = reply.ReadUint8();
    seekConfig.repateFilter = reply.ReadUint8();
    seekConfig.filterPolicy = reply.ReadUint8();
    seekConfig.phys = reply.ReadUint8();
    seekConfig.type[0] = reply.ReadUint8();
    seekConfig.interval[0] = reply.ReadUint16();
    seekConfig.window[0] = reply.ReadUint16();
    seekConfig.type[1] = reply.ReadUint8();
    seekConfig.interval[1] = reply.ReadUint16();
    seekConfig.window[1] = reply.ReadUint16();
    seekConfig.type[2] = reply.ReadUint8();
    seekConfig.interval[2] = reply.ReadUint16();
    seekConfig.window[2] = reply.ReadUint16();
    return (SleErrorCode)exception;
}

} // SleStandard
} // NearLink
} // OHOS